package webjoch.process.strategies.puzzles;

import java.awt.Graphics2D;

import org.rsbuddy.tabs.Inventory;

import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.Widgets;
import com.rsbuddy.script.task.Task;

import webjoch.methods.Items;
import webjoch.misc.Constants;
import webjoch.process.strategies.Puzzle;
import webjoch.wrappers.Room;

/**
 * 
 * @author Webjoch
 *
 */
public class DemagedConstruct extends Puzzle{
	Room room;
	public final int CRATE_MAGIC_ROCKS = 49543;
	
	public final int LUMP_OF_STONE = 17364;
	
	public final int REPAIRED_CONSTRUCT = 11005;
	public final int CONSTRUCT_ARM = 11002;
	public final int CONSTRUCT_HEAD = 11003;
	public final int CONSTRUCT_LEG = 11004;
	
	public final int ARM_U = 17365;
	public final int HEAD_U = 17367;
	public final int LEG_U = 17369;
	public final int[] ALL_U = {ARM_U, HEAD_U, LEG_U};
	
	public final int ARM = 17368;
	public final int HEAD = 17372;
	public final int LEG = 17374;
	public final int ALL[] = {ARM, HEAD, LEG};
	
	@Override
	public void setRoom(Room room) {
		this.room = room;
	}

	@Override
	public boolean isValid() {
		return room.getNpc(CONSTRUCT_ARM) != null || room.getNpc(CONSTRUCT_HEAD) != null ||room.getNpc(CONSTRUCT_LEG) != null || room.getNpc(REPAIRED_CONSTRUCT) != null;
	}

	@Override
	public Type getType() {
		return Type.DEMAGED_CONSTRUCT;
	}

	@Override
	public int loop(){
		Task.sleep(500, 1000);
		if (room.getNpc(REPAIRED_CONSTRUCT) != null){
			room.getNpc(REPAIRED_CONSTRUCT).interact("Charge");
			Task.sleep(15000);
			return 1;
		}
		if (room.getGroundItem(Constants.CHISEL[0]) != null && !Inventory.contains(Constants.CHISEL[0])){
			Items.interact(room.getGroundItem(Constants.CHISEL[0]), "Take");
			return 1;
		}
		
		if (Widgets.getComponent(451, 0).isVisible()){
			if (room.getNpc(CONSTRUCT_ARM) != null) Widgets.getComponent(451, 1).click();
			if (room.getNpc(CONSTRUCT_HEAD) != null) Widgets.getComponent(451, 3).click();
			if (room.getNpc(CONSTRUCT_LEG) != null) Widgets.getComponent(451, 2).click();
			return 1;
		}
		
		if (Inventory.contains(LUMP_OF_STONE)){
			Inventory.useItem(Inventory.getItem(Constants.CHISEL[0]), Inventory.getItem(LUMP_OF_STONE));
			for (int i = 0; i < 100; i++){
				if (Widgets.getComponent(451, 0).isVisible()) break;
				Task.sleep(30);
			}
			return 1;
		}
		
		if (Inventory.containsOneOf(ALL_U)){
			if (Inventory.contains(ARM_U)) Inventory.getItem(ARM_U).interact("Imbue");
			if (Inventory.contains(HEAD_U)) Inventory.getItem(HEAD_U).interact("Imbue");
			if (Inventory.contains(LEG_U)) Inventory.getItem(LEG_U).interact("Imbue");
			return 1;
		}
		
		if (Inventory.containsOneOf(ALL)){
			if (Inventory.contains(ARM) && room.getNpc(CONSTRUCT_ARM) != null) { Inventory.getItem(ARM).interact("use"); room.getNpc(CONSTRUCT_ARM).interact("Use"); }
			if (Inventory.contains(HEAD) && room.getNpc(CONSTRUCT_HEAD) != null) { Inventory.getItem(HEAD).interact("use"); room.getNpc(CONSTRUCT_HEAD).interact("Use"); }
			if (Inventory.contains(LEG) && room.getNpc(CONSTRUCT_LEG) != null) { Inventory.getItem(LEG).interact("use"); room.getNpc(CONSTRUCT_LEG).interact("Use"); }
			return 1;
		}
		
		if (!Inventory.contains(LUMP_OF_STONE) && room.getObject(CRATE_MAGIC_ROCKS) != null){
			if (Calculations.distanceTo(room.getObject(CRATE_MAGIC_ROCKS)) > 5) room.getObject(CRATE_MAGIC_ROCKS).getLocation().clickOnMap();
			else room.getObject(CRATE_MAGIC_ROCKS).interact("Take");
			return 1;
		}
		
		return 0;
	}

	@Override
	public int[] itemNeed() {
		return new int[] {Constants.HAMMER[0]};
	}

	@Override
	public boolean isFinished() {
		return false;
	}

	@Override
	public void messageReceived(MessageEvent e) {
		
	}

	@Override
	public void onRepaint(Graphics2D g) {
		// TODO Auto-generated method stub
		
	}

}
